/**
 * @author 蒙伟明
 * @data 2020年6月12日
 */
package com.ysxm.controller.admincontroller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ysxm.entity.User;
import com.ysxm.service.adminservice.AUserService;
import com.ysxm.service.userservice.UserService;
import com.ysxm.util.FileAccepterUtil;
import com.ysxm.util.ResponseInfo;

/**
 * @author mwm
 *
 */
@Controller
public class AUserController {

	@Autowired
	private UserService userService;
	@Autowired
	private AUserService auserService;

	// 添加用户
	@RequestMapping(value = "addAUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject addAUser(@RequestBody User user, HttpSession session) {
		JSONObject json = new JSONObject();

		int status = 0;
		String msg = "添加成功";
		System.out.println(user);
		if (user.getUsername() == null || user.getUsername().equals("")) {
			json.put("status", -1);
			json.put("msg", "用户名不能为空");
			return json;
		}

		if (user.getPassword() == null || user.getPassword().equals("")) {
			json.put("status", -1);
			json.put("msg", "密码不能为空");
			return json;
		}

		if (user.getPhone() == null || user.getPhone().equals("")) {
			json.put("status", -1);
			json.put("msg", "用户电话不能为空");
			return json;
		}

		if (user.getEmail() == null || user.getEmail().equals("")) {
			json.put("status", -1);
			json.put("msg", "用户邮箱不能为空");
			return json;
		}

		if (user.getAddress() == null || user.getAddress().equals("")) {
			json.put("status", -1);
			json.put("msg", "用户地址不能为空");
			return json;
		}
		try {
			if (!userService.isName(user.getUsername())) {
				json.put("status", -1);
				json.put("msg", "该用户名已存在");
				return json;
			}
			if (!userService.isPhone(user.getPhone())) {
				json.put("status", -1);
				json.put("msg", "该用户电话已存在");
				return json;
			}
			if (!userService.isEmail(user.getEmail())) {
				json.put("status", -1);
				json.put("msg", "该用户邮箱已存在");
				return json;
			}

		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("msg", "添加失败,请稍后再试");
			return json;
		}
		userService.userRegister(user);
		json.put("status", status);
		json.put("msg", msg);
		return json;
	}

	// 刪除用户
	@GetMapping(value = "delAUser", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<String> delAUser(Integer uid) {

		auserService.deleteAUser(uid);
		return new ResponseInfo<String>(ResponseInfo.SUCCESS_CODE, "删除用户成功");
	}

	// 根据用户名查询用户
	@GetMapping(value = "selectAUser", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<List<User>> selectAUser(String username) {

		List<User> aulist = auserService.findAUserListByName(username);
		return new ResponseInfo<List<User>>(ResponseInfo.SUCCESS_CODE, ("查詢成功"), aulist);

	}

	// 用户查看个人信息
	@GetMapping(value = "findAUser", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject findAUser(Integer uid) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "操作成功";
		System.out.print("id" + uid);
		User user = userService.lookupUser(uid);
		System.out.print(user);
		json.put("user", user);
		json.put("status", status);
		json.put("msg", msg);
		return json;
	}

	// 用户修改个人信息
	@RequestMapping(value = "updataAUser", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject updateAUser(@RequestParam(value = "file", required = false) MultipartFile file, User user,
			HttpServletRequest request, HttpServletResponse response) {
		JSONObject json = new JSONObject();
		int status = 0;
		String msg = "修改成功";
		try {
			String path = request.getServletContext().getRealPath("/upload/");
			System.out.println(path);
			FileAccepterUtil accepterUtil = new FileAccepterUtil();
			String fileName = accepterUtil.FileUpload(file, path);

			user.setUid(user.getUid());
			user.setUserhead(fileName);
			user.toString();

			userService.updateUser(user);
			System.out.println(user);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("status", -1);
			json.put("msg", "操作失败");
			return json;
		}

		json.put("status", status);
		json.put("msg", msg);
		json.put("user", user);
		return json;

	}

	// 显示所有用户信息
	@GetMapping(value = "aFindAllUser", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<PageInfo<User>> findAllAdminPage(
			@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize, HttpSession session,
			PageInfo<User> pageInfo) {

		List<User> list = auserService.findAllAUser(pageNum, pageSize);
		pageInfo = new PageInfo<User>(list);
		return new ResponseInfo<PageInfo<User>>(ResponseInfo.SUCCESS_CODE, "显示所有管理员信息", pageInfo);

	}

	// 显示所有用户信息
	@GetMapping(value = "aFindUserByName", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public ResponseInfo<PageInfo<User>> aFindUserByName(
			@RequestParam(name = "pageNum", required = true, defaultValue = "1") int pageNum,
			@RequestParam(name = "pageSize", required = true, defaultValue = "10") int pageSize, HttpSession session,
			PageInfo<User> pageInfo) {

		List<User> list = auserService.findAllAUser(pageNum, pageSize);
		pageInfo = new PageInfo<User>(list);
		return new ResponseInfo<PageInfo<User>>(ResponseInfo.SUCCESS_CODE, "显示所有管理员信息", pageInfo);

	}
}
